package com.jk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.jk.dao.CartDao;
import com.jk.entity.ClothEntity;
import com.jk.entity.Payment;
import com.jk.entity.UserEntity;
import com.jk.entity.cartOrderEntity;
import com.jk.service.CartService;
import com.jk.util.OrderCodeFactoryUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @详细介绍类情况.
 * @ClassName CartServiceImpl
 * @Author 王倚天
 * @Date 2022/6/1
 * @Version 1.0
 */
@Service
@SuppressWarnings("all")
public class CartServiceImpl implements CartService {

    @Autowired
    private CartDao cartDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * @方法名称:查询购物车
     * @名字 王倚天

     * @返回值 java.util.List<com.jk.entity.ShopCartEntity>
     * @异常
     * @时间 2022/5/31 16:01
     */
    @Override
    public List<ClothEntity> findShopCart() {
        String key="login";
        String o = (String) redisTemplate.opsForValue().get(key);
        UserEntity userEntity = JSONObject.parseObject(o,UserEntity.class);
        return cartDao.findShopCart(userEntity.getUserId());
    }

    /**
     * @方法名称:删除购物车商品
     * @名字 王倚天
     * @param id
     * @返回值 void
     * @异常
     * @时间 2022/5/31 20:39
     */
    @Override
    public void deleteById(Integer id) {
        cartDao.deleteById(id);
    }

    /**
     * @方法名称:批量删除购物车
     * @名字 王倚天
     * @param ids
     * @返回值 void
     * @异常
     * @时间 2022/6/1 14:22
     */
    @Override
    public void deleteByIds(String[] ids) {
        cartDao.deleteByIds(ids);
    }

    /**
     * @方法名称:回显
     * @名字 王倚天
     * @param id
     * @返回值 com.jk.entity.ClothEntity
     * @异常
     * @时间 2022/6/6 19:45
     */
    @Override
    public ClothEntity getcartById(Integer id) {
        return cartDao.getcartById(id);
    }

    /**
     * @方法名称:修改
     * @名字 王倚天
     * @param cloth
     * @返回值 void
     * @异常
     * @时间 2022/6/6 19:52
     */
    @Override
    public void updateById(ClothEntity cloth) {
        cartDao.updateById(cloth);
    }

    /**
     * @方法名称：购物车提交订单
     * @名字 王倚天
     * @param sumprice
 * @param ids
     * @返回值 void
     * @异常
     * @时间 2022/6/7 9:55
     */
    @Override
    public void submitOrder(Integer sumprice, String[] ids) {
        for (String A : ids) {
            System.out.println(A);
            //获取用户id
            String key="login";
            String o = (String) redisTemplate.opsForValue().get(key);
            UserEntity userEntity = JSONObject.parseObject(o, UserEntity.class);
            //获取订单号
            String orderCode = OrderCodeFactoryUtil.getOrderCode(userEntity.getUserId());
            System.out.println(orderCode);
            cartOrderEntity cartOrderEntity = new cartOrderEntity();
            cartOrderEntity.setOrderId(orderCode);
            cartOrderEntity.setShopId(A);
            cartOrderEntity.setUserId(userEntity.getUserId());
            cartOrderEntity.setSumPrice(sumprice);
            cartOrderEntity.setState(2);
            cartOrderEntity.setPurchaseDate(new Date());
            mongoTemplate.save(cartOrderEntity);
        }
        //String s = JSONObject.toJSONString(cartOrderEntity);
        //redisTemplate.opsForValue().set("订单"+orderCode,s);
        //redisTemplate.opsForHash().put("订单",cartOrderEntity,cartOrderEntity);
        //删除购物车商品
        cartDao.deleteByIdss(ids);
    }

    /**
     * 购买记录
     * @return
     */
    @Override
    public List<Payment> queryOrder() {
        List<cartOrderEntity> list = mongoTemplate.findAll(cartOrderEntity.class);
        for (cartOrderEntity cartOrderEntity : list) {
            cartOrderEntity cartOrderEntity1 = new cartOrderEntity();
            cartOrderEntity1.setOrderId( cartOrderEntity.getOrderId());
            cartOrderEntity1.setShopId(cartOrderEntity.getShopId());
            cartOrderEntity1.setUserId(cartOrderEntity.getUserId());
            cartOrderEntity1.setSumPrice(cartOrderEntity.getSumPrice());
            cartOrderEntity1.setState(cartOrderEntity.getState());
            cartOrderEntity1.setPurchaseDate(cartOrderEntity.getPurchaseDate());
            cartDao.addcarOrder(cartOrderEntity1);
        }
        //获取用户id
        String key="login";
        String o = (String) redisTemplate.opsForValue().get(key);
        UserEntity userEntity = JSONObject.parseObject(o, UserEntity.class);
        Integer userId = userEntity.getUserId();
        List<Payment>list1=cartDao.queryOrder(userId);
        return list1;
    }



}
